<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-21
 * Func   注册登录
 */
defined('IN_YuLin') || exit('NO PERMIT!');

if($_uid)
{
	header('Location: '.MU('user'));
	exit;
}

if(IS_POST)
{
    include_once LIB_PATH."class/Secoder.class.php";
	
    $username	=	NoBadStr($_POST['username']);
    $password	=	NoBadStr($_POST['password']);
    $password2	=	NoBadStr($_POST['password2']);
    $verify		=	NoBadStr($_POST['verify']);
	
	(strlen($username) < 6 || strlen($username) > 12) && AjaxReturn(2,'账号为6-12位');
	(strlen($password) < 6 || strlen($password) > 20) && AjaxReturn(2,'密码为6-20位');
	($password != $password2) && AjaxReturn(2,'确认密码不正确');

	(!Secoder::check($verify)) && AjaxReturn(1,'验证码错误');

    $user = $db->getrow('SELECT 1 FROM '.Table('user').' WHERE username = \''.$username.'\'');
	$user && AjaxReturn(2,'该账号已经被注册');
	
	//	插入数据库
	$ins = [];
	$ins['username']	=	$username;
	$ins['password']	=	Password($password);
	$ins['dateline']	=	TIMESTAMP;
	$ins['status']		=	0;
	$id = $db->add('user',$ins);
	!$id && AjaxReturn(1,'注册失败，请重新注册');

    $_SESSION['uid'] = $id;
	
    AjaxReturn(0,'注册成功');
}

$tpl->display('wap/register');